package com.myApp.db;

import java.util.ArrayList;

import android.content.Context;

import com.myApp.exception.RecordNotFoundException;
import com.myApp.pojo.Channel;

public class Channels extends ArrayList<Channel> {

	private static final long serialVersionUID = -4330782734675795279L;
	
	private static Channels instance = null;
	
	private Channels(){}
	
	public static Channels getInstance(Context ct){
		
		if (instance == null){
			
			instance = new Channels();
			
			instance.load(ct);
		}
		
		return instance;
	}
	
	/**
	 * 分类数据抽出
	 * 
	 * @param ct 上下文
	 */
	public boolean load(Context ct){
		
		DatabaseHelper dbHelper = new DatabaseHelper(ct);
		
		return dbHelper.loadAll(this);
	}
	
	/**
	 * 取得指定分类的频道
	 * 
	 * @param categoryId 分类ID
	 */
	public ArrayList<Channel> getChannelsOfCategory(int categoryId){
		
		ArrayList<Channel> channels = new ArrayList<Channel>();
		
		for (Channel channel : this){
			
			if (channel.getChannelCategory() == categoryId){
				
				channels.add(channel);
			}
		}
		
		if (channels.size() == 0){
			
			throw new RecordNotFoundException(this, String.valueOf(categoryId));
		}
		
		return channels;
	}
	
}
